<template>
  <div class="wrap">
    <!-- <div class="show-title">场馆代码：{{ site_code }}</div>
    <div class="show-title">场馆名称：{{ site_name }}</div> -->
    <el-form
      ref="ruleFormRef"
      style="max-width: 600px"
      :model="ruleForm"
      status-icon
      :rules="rules"
      label-width="auto"
      class="ruleForm-wrap"
    >
      <el-form-item label="区块代码" prop="area_code">
        <el-input v-model="ruleForm.area_code" disabled />
      </el-form-item>
      <el-form-item label="区块名称" prop="area_name">
        <el-input v-model="ruleForm.area_name" />
      </el-form-item>
      <el-form-item label="区块坐标-左上角x" prop="left_up_x">
        <el-input v-model.number="ruleForm.left_up_x" />
      </el-form-item>
      <el-form-item label="区块坐标-左上角y" prop="left_up_y">
        <el-input v-model.number="ruleForm.left_up_y" />
      </el-form-item>
      <el-form-item label="区块坐标-右下角x" prop="right_down_x">
        <el-input v-model.number="ruleForm.right_down_x" />
      </el-form-item>
      <el-form-item label="区块坐标-右下角y" prop="right_down_y">
        <el-input v-model.number="ruleForm.right_down_y" />
      </el-form-item>
      <el-form-item label="水深区块标记">
        <el-select
          v-model.number="ruleForm.water_depth"
          @change="handleChange"
          placeholder="请选择"
        >
          <el-option
            v-for="item in water_depth_arr"
            :key="item"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item class="btn-wrap">
        <el-button type="primary" @click="submitForm(ruleFormRef)">
          修改
        </el-button>
        <el-button type="warning" plain @click="resetForm(ruleFormRef)"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>
import { reactive, ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { postData } from '@/http/request.js'
import { useRouter, useRoute } from 'vue-router'
let router = useRouter()
let route = useRoute()
const routeParams = route.query.area_code // 获取路由参数
let ruleFormRef = ref(null)
let water_depth_arr = ref([
  { label: '水上区域', value: 0 },
  { label: '浅水区', value: 1 },
  { label: '深水区', value: 2 },
])

const ruleForm = reactive({
  area_code: routeParams,
  area_name: '',
  left_up_x: '',
  left_up_y: '',
  right_down_x: '',
  right_down_y: '',
  water_depth: '',
})

const rules = reactive({
  area_code: [{ required: false, trigger: 'blur' }],
  area_name: [{ required: true, message: '请输入场馆名称', trigger: 'blur' }],
  left_up_x: [{ required: false, message: '请输入', trigger: 'blur' }],
  left_up_y: [{ required: false, message: '请输入', trigger: 'blur' }],
  right_down_x: [{ required: false, message: '请输入', trigger: 'blur' }],
  right_down_y: [{ required: false, message: '请输入', trigger: 'blur' }],
  water_depth: [{ required: false, message: '请选择', trigger: 'blur' }],
})

let handleChange = (val) => {
  ruleForm.water_depth = val
  console.log(ruleForm.water_depth)
}

let init_info = () => {
  postData('/manage/site/area/info', {
    area_code: routeParams,
  })
    .then((res) => {
      if (res.code == 0) {
        ruleForm.area_code = res.datas.area_code
        ruleForm.area_name = res.datas.area_name
        ruleForm.left_up_x = res.datas.left_up_x
        ruleForm.left_up_y = res.datas.left_up_y
        ruleForm.right_down_x = res.datas.right_down_x
        ruleForm.right_down_y = res.datas.right_down_y
        ruleForm.water_depth = res.datas.water_depth
      }
    })
    .catch((err) => {
      console.log(err)
    })
}

//bug，此时提交的时候，water_depth 有时候为空，有时候不是
const submitForm = async (formEl) => {
  console.log(ruleForm.water_depth)
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      postData('/manage/site/area/update', {
        area_code: ruleForm.area_code,
        area_name: ruleForm.area_name,
        left_up_x: ruleForm.left_up_x ? ruleForm.left_up_x : '',
        left_up_y: ruleForm.left_up_y ? ruleForm.left_up_y : '',
        right_down_x: ruleForm.right_down_x ? ruleForm.right_down_x : '',
        right_down_y: ruleForm.right_down_y ? ruleForm.right_down_y : '',
        water_depth: ruleForm.water_depth ? ruleForm.water_depth : '',
      })
        .then((res) => {
          if (res.code == 0) {
            ElMessage({
              type: 'success',
              message: '修改成功',
            })
            setTimeout(() => {
              router.push('/site/siteArea?site_code=' + routeParams)
            }, 3000)
          } else {
            ElMessage({
              type: 'error',
              message: '修改失败',
            })
          }
        })
        .catch((err) => {
          console.log(err)
        })
    } else {
      console.log('error submit!', fields)
    }
  })
}

const resetForm = (formEl) => {
  if (!formEl) return
  formEl.resetFields()
}

onMounted(() => {
  init_info()
})
</script>
<style scope>
.wrap {
  margin: 20px 0 0 20px;
}

.title {
  width: 100%;
  height: 40px;
  line-height: 40px;
  position: relative;
  /* background: #c0dee2; */
  padding-left: 30px;
  font-size: 14px;
  margin: 6px 0 20px 0;
  /* border-radius: 12px 12px 0 0; */
  /* font-weight: bold; */
}

.title::before {
  content: '';
  width: 2px;
  height: 16px;
  background: #676a6d;
  position: absolute;
  top: 12px;
  left: 15px;
}

.title span {
  float: left;
  color: #676a6d;
  margin-left: 25px;
}
.show-title {
  height: 30px;
  line-height: 30px;
  color: #676a6d;
  font-size: 14px;
}
.btn-wrap {
  margin-top: 30px;
}
.ruleForm-wrap {
  margin-top: 20px;
}
</style>
